package thoven.achievement.net.ideapluginachievement.service

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.diagnostic.logger
import com.intellij.util.xmlb.XmlSerializerUtil

/**
 * 插件个性化设置服务
 */
@State(
    name = "AchievementPluginSettings",
    storages = [Storage("achievement-plugin-settings.xml")]
)
class SettingsService : PersistentStateComponent<SettingsService.State> {
    
    companion object {
        fun getInstance(): SettingsService = ApplicationManager.getApplication().getService(SettingsService::class.java)
        private val LOG = logger<SettingsService>()
    }
    
    data class State(
        // 通知设置
        var enableNotifications: Boolean = true,
        var enableSoundNotifications: Boolean = false,
        var enableStatusBarNotifications: Boolean = true,
        var enablePopupNotifications: Boolean = true,
        
        // 状态栏设置
        var statusBarFormat: StatusBarFormat = StatusBarFormat.DETAILED,
        var showProgressInStatusBar: Boolean = true,
        
        // 主题设置
        var themeColor: ThemeColor = ThemeColor.DEFAULT,
        var enableGradientCards: Boolean = true,
        var enableAnimations: Boolean = true,
        
        // 成就难度设置
        var achievementDifficulty: AchievementDifficulty = AchievementDifficulty.STANDARD,
        
        // 工作时间设置
        var enableWorkTimeReminder: Boolean = false,
        var workStartHour: Int = 9,
        var workEndHour: Int = 18,
        var enableWeekendReminder: Boolean = true,
        
        // 健康提醒设置
        var enableHealthReminder: Boolean = true,
        var continuousCodingWarningMinutes: Int = 120, // 2小时
        var restReminderIntervalMinutes: Int = 60, // 1小时
        
        // 数据隐私设置
        var enableDataCollection: Boolean = true,
        var enableUsageAnalytics: Boolean = false
    )
    
    /**
     * 状态栏显示格式
     */
    enum class StatusBarFormat(val displayName: String, val description: String) {
        SIMPLE("简洁", "仅显示今日行数"),
        DETAILED("详细", "显示今日和累计数据"),
        PROGRESS("进度", "显示最近成就进度"),
        CUSTOM("自定义", "用户自定义格式")
    }
    
    /**
     * 主题色彩方案
     */
    enum class ThemeColor(val displayName: String, val primaryColor: String, val accentColor: String) {
        DEFAULT("默认蓝色", "#4A90E2", "#357ABD"),
        GREEN("清新绿色", "#5CB85C", "#449D44"), 
        PURPLE("典雅紫色", "#9B59B6", "#8E44AD"),
        ORANGE("活力橙色", "#F39C12", "#E67E22"),
        RED("热情红色", "#E74C3C", "#C0392B"),
        DARK("暗夜主题", "#34495E", "#2C3E50")
    }
    
    /**
     * 成就难度等级
     */
    enum class AchievementDifficulty(val displayName: String, val multiplier: Double, val description: String) {
        CASUAL("休闲", 0.7, "降低成就门槛，更容易获得满足感"),
        STANDARD("标准", 1.0, "平衡的成就体验"),
        HARDCORE("困难", 1.5, "更有挑战性的成就目标"),
        EXTREME("极限", 2.0, "为硬核程序员准备的极限挑战")
    }
    
    private var myState = State()
    
    override fun getState(): State = myState
    
    override fun loadState(state: State) {
        XmlSerializerUtil.copyBean(state, myState)
        LOG.info("加载插件设置: 通知=${myState.enableNotifications}, 主题=${myState.themeColor}, 难度=${myState.achievementDifficulty}")
    }
    
    // 便捷访问方法
    fun isNotificationsEnabled(): Boolean = myState.enableNotifications
    fun isSoundEnabled(): Boolean = myState.enableSoundNotifications  
    fun isStatusBarNotificationsEnabled(): Boolean = myState.enableStatusBarNotifications
    fun isPopupNotificationsEnabled(): Boolean = myState.enablePopupNotifications
    
    fun getStatusBarFormat(): StatusBarFormat = myState.statusBarFormat
    fun shouldShowProgressInStatusBar(): Boolean = myState.showProgressInStatusBar
    
    fun getThemeColor(): ThemeColor = myState.themeColor
    fun isGradientEnabled(): Boolean = myState.enableGradientCards
    fun isAnimationsEnabled(): Boolean = myState.enableAnimations
    
    fun getAchievementDifficulty(): AchievementDifficulty = myState.achievementDifficulty
    fun getDifficultyMultiplier(): Double = myState.achievementDifficulty.multiplier
    
    fun isWorkTimeReminderEnabled(): Boolean = myState.enableWorkTimeReminder
    fun getWorkHours(): Pair<Int, Int> = Pair(myState.workStartHour, myState.workEndHour)
    fun isWeekendReminderEnabled(): Boolean = myState.enableWeekendReminder
    
    fun isHealthReminderEnabled(): Boolean = myState.enableHealthReminder
    fun getContinuousCodingWarningMinutes(): Int = myState.continuousCodingWarningMinutes
    fun getRestReminderIntervalMinutes(): Int = myState.restReminderIntervalMinutes
    
    fun isDataCollectionEnabled(): Boolean = myState.enableDataCollection
    fun isUsageAnalyticsEnabled(): Boolean = myState.enableUsageAnalytics
    
    /**
     * 检查当前是否在工作时间
     */
    fun isWorkTime(): Boolean {
        if (!myState.enableWorkTimeReminder) return true
        
        val now = java.time.LocalTime.now()
        val currentHour = now.hour
        
        return currentHour in myState.workStartHour until myState.workEndHour
    }
    
    /**
     * 检查是否是周末且启用了周末提醒
     */
    fun shouldShowWeekendReminder(): Boolean {
        if (!myState.enableWeekendReminder) return false
        
        val dayOfWeek = java.time.LocalDate.now().dayOfWeek
        return dayOfWeek == java.time.DayOfWeek.SATURDAY || dayOfWeek == java.time.DayOfWeek.SUNDAY
    }
    
    /**
     * 根据难度调整成就目标值
     */
    fun adjustAchievementTarget(baseTarget: Int): Int {
        return (baseTarget / getDifficultyMultiplier()).toInt().coerceAtLeast(1)
    }
}
